package com.da.web.school_class.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.da.common.Result;
import com.da.web.school_class.entity.AssignClass;
import com.da.web.school_class.entity.AssignClassParm;
import com.da.web.school_class.entity.ListParm;
import com.da.web.school_class.entity.SchoolClass;
import com.da.web.school_class.sevice.SchoolClassService;
import com.da.web.school_major.entity.SchoolMajor;
import com.da.web.school_major.service.SchoolMajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author da
 * @time 2023/12/5 下午 5:26
 */
@RestController
@RequestMapping("/api/class")
public class SchoolClassController {
    @Autowired
    private SchoolClassService schoolClassService;
    @Autowired
    private SchoolMajorService schoolMajorService;

    //新增
    @PostMapping
    public Result add(@RequestBody SchoolClass schoolClass) {
        boolean save = schoolClassService.save(schoolClass);
        if (save) {
            return Result.success("新增成功");
        }
        return Result.error("新增失败");
    }

    //编辑
    @PutMapping
    public Result edit(@RequestBody SchoolClass schoolClass) {
        boolean save = schoolClassService.updateById(schoolClass);
        if (save) {
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }

    //编辑回显
    @GetMapping("/getClassById")
    public Result getClassById(Long classId) {
        SchoolClass schoolClass = schoolClassService.getSchoolClassById(classId);
        return Result.success("查询成功", schoolClass);
    }

    //删除
    @DeleteMapping("/{classId}")
    public Result delete(@PathVariable("classId") Long classId) {
        boolean b = schoolClassService.removeById(classId);
        if (b) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    //列表查询
    @GetMapping("/list")
    public Result getList(ListParm parm) {
        IPage<SchoolClass> list = schoolClassService.getList(parm);
        return Result.success("查询成功", list);
    }

    //根据学院id查询专业列表
    @GetMapping("/getMajorList")
    public Result getMajorList(Long collageId) {
        QueryWrapper<SchoolMajor> query = new QueryWrapper<>();
        query.lambda().eq(SchoolMajor::getCollageId, collageId);
        List<SchoolMajor> list = schoolMajorService.list(query);
        return Result.success("查询成功", list);
    }

    //分配宿舍,查询班级列表
    @GetMapping("/getAssignClass")
    public Result getAssignClass(AssignClassParm parm) {
        IPage<AssignClass> list = schoolClassService.getAssignClass(parm);
        return Result.success("查询成功", list);
    }

    @GetMapping("/getListForAssign")
    public Result getListForAssign() {
        List<SchoolClass> list = schoolClassService.list();
        return Result.success("查询成功", list);
    }

}
